package cn.janyd.jmall.dubbo.core.config;

import cn.janyd.jmall.dubbo.core.web.DubboI18nInterceptor;
import cn.janyd.jmall.dubbo.core.web.I18nResultInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @author janyd
 * @since 2020/8/5 20:10
 */
@Configuration
@EnableConfigurationProperties(DubboVersionProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class DubboWebAutoConfiguration implements WebMvcConfigurer {

    @Bean
    public ResponseBodyAdvice<?> responseBodyAdvice(MessageSource messageSource) {
        return new I18nResultInterceptor(new MessageSourceAccessor(messageSource));
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new DubboI18nInterceptor());
    }
}
